package cn.sunzq.sqlite.demo.demo.contreller;

import cn.sunzq.sqlite.demo.base.response.ResponseDTO;
import cn.sunzq.sqlite.demo.entity.sqlite1.TempLincolnshireShort;
import cn.sunzq.sqlite.demo.service.sqlite1.ITempLincolnshireShortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.StatementCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @创建人 孙中泉
 * @创建时间 2021/8/3
 * @描述
 */
@RestController
@RequestMapping("/queryDemo")
public class DemoController {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ITempLincolnshireShortService tempLincolnshireShortService;

    @RequestMapping(value = "/demo1", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResponseDTO<Boolean> demo1(){
        return ResponseDTO.defaultResponse(tempLincolnshireShortService.list().subList(0,10));
    }

    @RequestMapping(value = "/demo2", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResponseDTO<Boolean> demo2(){
        Integer id = jdbcTemplate.query("SELECT seq FROM sqlite_sequence WHERE name = 'temp_lincolnshire_short'", resultSet -> {return resultSet.getInt("seq");
        });
        id++;
        return ResponseDTO.defaultResponse(tempLincolnshireShortService.save(new TempLincolnshireShort().setDay("11").setHours("11").setMonth("01").setName("11").setYear("1991").setValue(new BigDecimal(11)).setId(id)));
    }

}
